import urlConfig from './config.js'
import store from './../store'

const request = {}
const headers = {}
const PORT1 = '/baseinfo'


request.globalRequest = (url, method, data, power, hideLoading) => {
	console.log(data)
	console.log('data')
	/*     权限判断 因为有的接口请求头可能需要添加的参数不一样，所以这里做了区分
	== 不通过access_token校验的接口
	== 文件下载接口列表
	== 验证码登录 */
	//接口请求
	if (!hideLoading) {
		uni.showLoading({
			mask: true,
			title: '请稍候...'
		})
	}

	switch (power) {
		case 1:
			headers['Content-type'] = 'application/json'
			headers['Authorization'] = (store.state.user.access_token) ? 'Bearer ' + `${store.state.user.access_token}` : ''
			break;
		case 2:
			headers['Authorization'] = 'Basic a3N1ZGlfcGM6a3N1ZGlfcGM='
			break;
		case 3:
			responseType = 'blob'
			break;
		case 4:
			headers['content-type'] = 'application/json'
			headers['token'] = (store.state.user.access_token) ? `${store.state.user.access_token}` : ''
		default:
			headers['content-type'] = 'application/json'
			headers['token'] = (store.state.user.access_token) ? `${store.state.user.access_token}` : ''
			break;
	}

	return uni.request({
		url: urlConfig.url_config + url,
		method,
		data: data,
		dataType: 'json',
		header: headers
	}).then(res => {
		!hideLoading && uni.hideLoading()
		
		console.log(res)
		if(res[1].data.code + '' === '500') {
			/* store.commit('LOGOUT'); */
			uni.showToast({
				title: res[1].data.msg,
				icon: 'none'
			})
			// return
		}
		if(res[1].data.msg && res[1].data.code + '' === '401') {
			/* store.commit('LOGOUT'); */
			console.log("=======================111111111111")
			uni.reLaunch({
				url: '/pages/login',
				fail(err) {
					console.log(err)
				}
				
			})
		}
		if(res[1].data.msg && res[1].data.code + '' === '402') {
			store.commit('DRIVERLOGOUT');
			console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
			uni.navigateTo({
				url: '/pages/driverlogin/driverlogin',
				fail(err) {
					console.log(err)
				}
				
			})
		}
		if(res[1].data.code + '' !== '200') {
			uni.showToast({
				title: res[1].data.msg,
				icon: 'none'
			})
		}
		
		if(res[1].data.code + '' === '200') {
			return res[1].data;
		}
		
		
	}).catch(parmas => {
		!hideLoading && uni.hideLoading()
		uni.showToast({
			title: "连接失败",
			icon: 'none'
		})
	})
}

export default request
